package com.group.controller;

import com.group.entity.CommonResult;
import com.group.entity.wms.WarehouseEntity;
import com.group.service.WarehouseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Resource
    private WarehouseService warehouseService;

    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('warehouse')")
    public CommonResult<?> findAll(){
        return CommonResult.success(this.warehouseService.findAll(),null);
    }


    @PostMapping("/add")
    @PreAuthorize("hasAuthority('warehouse/add')")
    public CommonResult<?> add(@RequestBody WarehouseEntity warehouseEntity){
       return this.warehouseService.add(warehouseEntity)>0?CommonResult.success(null,"仓库创建成功"):CommonResult.error(499,"网络异常");
    }

    @PutMapping("/update")
    @PreAuthorize("hasAuthority('warehouse/update')")
    public CommonResult<?> update(@RequestBody WarehouseEntity warehouseEntity){
        return this.warehouseService.update(warehouseEntity)>0?CommonResult.success(null,"仓库更新成功"):CommonResult.error(499,"网络异常");
    }

    @DeleteMapping("/delete/{warehouseId}")
    @PreAuthorize("hasAuthority('warehouse/delete')")
    public CommonResult<?> delete(@PathVariable("warehouseId")Long warehouseId){
        return this.warehouseService.delete(warehouseId)>0?CommonResult.success(null,"仓库删除成功"):CommonResult.error(499,"网络异常");
    }

    @GetMapping("findByNameLike")
    @PreAuthorize("hasAuthority('warehouse/findByNameLike')")
    public CommonResult<?> findByNameLike(@RequestParam("keyWorld")String keyWorld){
        return CommonResult.success(this.warehouseService.findByNameLike(keyWorld),"请求成功");
    }

}
